package com.funtonow.jqdk.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.funtonow.jqdk.R;
import com.funtonow.jqdk.adapter.ZxAdapter;
import com.funtonow.jqdk.base.BaseFragment;
import com.funtonow.jqdk.base.UrlUtils;
import com.funtonow.jqdk.bean.NewsBean;
import com.funtonow.jqdk.ui.ZxWebViewActivity;
import com.funtonow.jqdk.utils.HttpUtil;
import com.funtonow.jqdk.widget.AutoSwipeRefreshView;

import java.io.IOException;
import java.util.List;

import butterknife.BindView;
import okhttp3.Call;

/**
 * @author Jiajien
 * @date 2019/8/2
 * 贷款攻略
 */
public class ZXStrategyFragment extends BaseFragment {

    @BindView(R.id.rlv_zx)
    RecyclerView rlvZz;
    @BindView(R.id.auto_refresh)
    AutoSwipeRefreshView autoSwipeRefreshView;


    private ZxAdapter zxAdapter;
    private List<NewsBean> newsBeans;

    public static ZXStrategyFragment newInstance() {
        Bundle args = new Bundle();
        ZXStrategyFragment fragment = new ZXStrategyFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View getLayout(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_recommend, null);
    }

    @Override
    public void onCreateFragment(@Nullable Bundle savedInstanceState) {
        initRecyclerView(rlvZz);
        zxAdapter = new ZxAdapter();
        rlvZz.setAdapter(zxAdapter);
        getZxData();
        zxAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(getActivity(), ZxWebViewActivity.class);
                intent.putExtra("title",zxAdapter.getItem(position).getTitle());
                intent.putExtra("url",zxAdapter.getItem(position).getUrl());
                startActivity(intent);
            }
        });
        autoSwipeRefreshView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getZxData();
            }
        });
        autoSwipeRefreshView.autoRefresh();
    }

    private void initRecyclerView(RecyclerView recyclerView) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //设置为垂直布局，这也是默认的
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        //设置增加或删除条目的动画
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }

    /**
     * 获取数据appId
     */
    private void getZxData() {
        HttpUtil.getInstance().getDataAsynFromNet(UrlUtils.ZX_URL, new HttpUtil.MyNetCall() {
            @Override
            public void success(Call call, String jsonString) throws IOException {
                JSONObject jsonObject = JSON.parseObject(jsonString);
                newsBeans = JSON.parseArray(jsonObject.getString("gonglue"), NewsBean.class);
                autoSwipeRefreshView.setRefreshing(false);
                zxAdapter.setNewData(newsBeans);
            }

            @Override
            public void failed(Call call, IOException e) {

            }
        });
    }


}
